package flower.com.a600_xm.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.util.List;
import java.util.UUID;


import flower.com.a600_xm.db.dbbean.Buy_Shopping;


/**
 * Created by luoliwen on 16/4/28.
 * 完成对用戶数据库的CRUD的操作
 */
public class Buy_ShoppingInfoDao {
    private Context context;
    private Dao<Buy_Shopping, Integer> userDao;
    private Z_DataBaseHelper helper;

    public Buy_ShoppingInfoDao(Context context) {
        this.context = context;
        helper = Z_DataBaseHelper.getInstance(context);
        try {
            userDao = helper.getDao(Buy_Shopping.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addShopping(String Name, String Price, String Number


    ) {
        try {
            Buy_Shopping user = new Buy_Shopping();
            user.setId(UUID.randomUUID().toString());
            user.setName(Name);
            user.setPrice(Price);
            user.setNumber(Number);
            userDao.create(user);

        } catch (Exception e) {
            e.printStackTrace();
            Log.e("88888", "" + e);
        }
    }

    List<Buy_Shopping> life_shoppings;

    public List<Buy_Shopping> query() {
        try {
            life_shoppings = userDao.queryForAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return life_shoppings;
    }

    Buy_Shopping life_shopping;

    public void deleteUser(String name) {
        try {
            life_shopping = userDao.queryBuilder().where().eq("name", name).queryForFirst();
            userDao.delete(life_shopping);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void querys(String name) {
        try {
            life_shoppings = userDao.queryBuilder().where().eq("name", name).query();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteUsers() {
        try {
            userDao.delete(query());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public List<Buy_Shopping> query(String type) {
        try {
            life_shoppings = userDao.queryBuilder().where().eq("type", type).query();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return life_shoppings;
    }

    public List<Buy_Shopping> updata(String name, String number) {
        try {
            UpdateBuilder<Buy_Shopping, Integer> life_devicesIntegerUpdateBuilder = userDao.updateBuilder();
            life_devicesIntegerUpdateBuilder.updateColumnValue("number", number).where().eq("name", name);
            life_devicesIntegerUpdateBuilder.update();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return life_shoppings;
    }


}
